<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>掌上工单</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.min.css">
		<style>
			html,
			body {
				background-color: #efeff4;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" style="display: none;">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">任务大厅</h1>
		</header>
		<nav class="mui-bar mui-bar-tab">
			<a id="defaultTab" class="mui-tab-item mui-active" href="subpage-home.html" data-type="task">
				<span class="mui-icon mui-icon-home" id="mui-icon-home"></span>
				<span class="mui-tab-label" data-label='home'>任务大厅</span>
			</a>
			<a class="mui-tab-item" href="subpage-notice.html" data-type="notice">
				<span class="mui-icon mui-icon-chatbubble" id="mui-icon-chatbubble"></span>
				<span class="mui-tab-label" data-label='notice'>公告</span>
			</a>
			<a class="mui-tab-item" href="subpage-setting.html" data-type="setting">
				<span class="mui-icon mui-icon-gear"></span>
				<span class="mui-tab-label" data-label='setting'>设置</span>
			</a>
		</nav>
		<script src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/md5.js" ></script>
		<script type="text/javascript" src="js/uuid.js" ></script>
		<script type="text/javascript" src="js/utils.js" ></script>
		<script>
			var tips = '<span class="mui-badge">{count}</span>' ;
			//mui初始化
			mui.init();
			var subpages = ['subpage-home.html', 'subpage-notice.html','subpage-setting.html'];
			var subpage_style = {
				top: '0',
				bottom: '51px'
			};
			
			var aniShow = {};
			
			 //创建子页面，首个选项卡页面显示，其它均隐藏；
			mui.plusReady(function() {
				
				var self = plus.webview.currentWebview();
				var indexPage = plus.webview.getWebviewById('index') ;
				mui.each(subpages, function(index, subpage) {
					var temp = {};
					var sub = plus.webview.create(subpage, subpage, subpage_style);
					if (index > 0) {
						sub.hide();
					}else{
						temp[subpage] = "true";
						mui.extend(aniShow,temp);
					}
					self.append(sub);
				});
				
				
				// 监听在线消息事件
			    plus.push.addEventListener("receive",function( msg ) {
			    	// Apple APNS message
			        if (msg.aps) {
			        	alert("接收到在线APNS消息：");
			        } else {
			           	var msgData = JSON.parse(msg.content) ||{};
						if(msgData && msgData.newTaskCount && msgData.newTaskCount != '0' ){
							document.getElementById('mui-icon-home').innerHTML = tips.replace('{count}','new');
						}
						if(msgData &&　msgData.newNoticeCount &&　msgData.newNoticeCount != '0'){
							document.getElementById('mui-icon-chatbubble').innerHTML = tips.replace('{count}','new');
						}
						if(msgData &&　msgData.newVersion &&　msgData.newVersion != '0'){
							xhz.update(false) ;
						}
			        }
			    },false);
			
				 //当前激活选项
				var activeTab = subpages[0];
				
				 //选项卡点击事件
				mui('.mui-bar-tab').on('tap', 'a', function(e) {
					var targetTab = this.getAttribute('href');
					var datatype = this.getAttribute('data-type');
					
//					console.debug('datatype ' + datatype) ;
					
					if (targetTab == activeTab) {
						return;
					}
					
					//显示目标选项卡
					if(mui.os.ios||aniShow[targetTab]){
						plus.webview.show(targetTab);
					}else{
						var temp = {};
						temp[targetTab] = "true";
						mui.extend(aniShow,temp);
						plus.webview.show(targetTab);
	//					plus.webview.show(targetTab,"fade-in",300);
					}
					//隐藏当前;
					plus.webview.hide(activeTab);
					//更改当前活跃的选项卡
					activeTab = targetTab;
					
					//更换标题
					var titleTab = this.querySelector('.mui-tab-label').getAttribute('data-label');
					if(indexPage){
						mui.fire(indexPage,'title:set',{title:titleTab});
					}
					
					if('notice' == datatype){
						var noticePage = plus.webview.getWebviewById('subpage-notice.html');
						mui.fire(noticePage, "notice:loading");
					}
					
				});
				 //自定义事件，模拟点击“首页选项卡”
				document.addEventListener('gohome', function() {
					var defaultTab = document.getElementById("defaultTab");
					//模拟首页点击
					mui.trigger(defaultTab, 'tap');
					//切换选项卡高亮
					var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
					if (defaultTab !== current) {
						current.classList.remove('mui-active');
						defaultTab.classList.add('mui-active');
					}
				});
				
				setTips = function(e){
					if(e.detail.id){
						document.getElementById(e.detail.id).innerHTML = tips.replace('{count}','new');
					}
				};
				clearTips = function(e){
					if(e.detail.id){
						document.getElementById(e.detail.id).innerHTML = '';
					}
				}
				//关闭tasktips
				window.addEventListener("tips:set", setTips);
				//关闭noticetips
				window.addEventListener("tips:clear", clearTips);
				
			}); 
		</script>
	</body>
</html>
